package com.example.function;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.adapter.StudentAdapter;
import com.example.adapter.TeacherAdapter;
import com.example.database.DatabaseHelper;
import com.example.pojo.Student;
import com.example.pojo.Teacher;
import com.example.student.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class StaffFunction extends AppCompatActivity {
    private DatabaseHelper databaseHelper;

    private Spinner spinnerJob, spinnerCollege;
    private RecyclerView recyclerViewTeacher;
    private List<Teacher> teacherList = new ArrayList<>();
    private TeacherAdapter teacherAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_staff_function);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        databaseHelper = new DatabaseHelper(this);
        // 初始化视图
        initViews();
        // 初始化学院和职位数据
        initCollegeJobData();
        // 初始化教师列表
        initTeacherList();

        // 监听学院 Spinner 的选择事件
        spinnerCollege.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCollege = (String) parent.getItemAtPosition(position);
                filterTeachers(selectedCollege, (String) spinnerJob.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        // 监听职位 Spinner 的选择事件
        spinnerJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedJob = (String) parent.getItemAtPosition(position);
                filterTeachers((String) spinnerCollege.getSelectedItem(), selectedJob);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        // 在 StudentFunctionActivity 或 StudentFunctionFragment 中
        FloatingActionButton fabAddStudent = findViewById(R.id.fab_add_teacher);
        fabAddStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到添加学生的表单页面
                Intent intent = new Intent(StaffFunction.this, AddStaff.class);
                startActivity(intent);
            }
        });
    }

    private void initViews() {
        spinnerCollege = findViewById(R.id.spinnerCollege);
        spinnerJob = findViewById(R.id.spinnerJob);

        recyclerViewTeacher = findViewById(R.id.recyclerViewTeachers);

        // 设置 RecyclerView
        recyclerViewTeacher.setLayoutManager(new LinearLayoutManager(this));
        teacherAdapter = new TeacherAdapter(teacherList, databaseHelper);
        recyclerViewTeacher.setAdapter(teacherAdapter);

    }
    /**
     * 初始化学院和职位数据
     */
    private void initCollegeJobData() {
        // 初始化学院下拉框
        List<String> colleges = new ArrayList<>();
        colleges.add("请选择学院"); // 添加“请选择”选项
        colleges.addAll(List.of("计算机科学与技术", "商学院", "文学院", "理学院", "工程学院", "医学院", "艺术学院", "法学院", "教育学院", "农学院"));
        ArrayAdapter<String> collegeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, colleges);
        collegeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCollege.setAdapter(collegeAdapter);

        // 初始化职位下拉框
        List<String> jobs = new ArrayList<>();
        jobs.add("请选择职位"); // 添加“请选择”选项
        jobs.addAll(List.of("教授", "副教授", "讲师", "助教"));
        ArrayAdapter<String> jobAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, jobs);
        jobAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerJob.setAdapter(jobAdapter);
    }
    /**
     * 初始化教师列表
     */
    private void initTeacherList() {
        // 从数据库获取所有教师
        teacherList.clear();
        List<Teacher> teachers = databaseHelper.getAllTeachers();
        teacherList.addAll(teachers);
        teacherAdapter.notifyDataSetChanged();
    }
    /**
     * 根据学院和职位筛选教师
     */
    private void filterTeachers(String college, String job) {
        // 如果选择的是“请选择”，则清空条件
        if ("请选择学院".equals(college)) {
            college = "";
        }
        if ("请选择职位".equals(job)) {
            job = "";
        }

        // 从数据库获取符合条件的教师
        teacherList.clear();
        teacherList.addAll(databaseHelper.getTeachersByFilter(college, job));
        teacherAdapter.notifyDataSetChanged();
    }

}